/*
Copyright (c) 2012 the VATSIM Connector project

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef __XPINN__PLANES_H__
#define __XPINN__PLANES_H__

#include "xpinn.h"
#include <messages.h>
#include <XPLMScenery.h>
#include <map>
#include <set>
#include <array>
#include <string>
#include "datarefs.h"
#include "callbacks.h"
#include "planestax.h"
#include "object.h"

enum
{
  XPINN_PLANE_DATAREFS = 19,
  XPINN_PLANE_OVERRIDES = 20,
};

class XPInnPlanes
{
public:
  XPInnPlanes();
  ~XPInnPlanes();

  void init();

  void acquireControl();
  void releaseControl();

  void handleMessage(VconMsgPlanePosition const& msg);
  void updatePositions();

private:
  XPInnPlanesTaxonomy m_taxonomy;

  bool m_wantsControl;
  bool m_hasControl;

  static void controlAvailableCallback(void* refcon);

  void invalidateObjects();

  void drawCallback();
  XPInnDrawCallback m_drawCallback;

  void drawBeforeGaugesCallback();
  XPInnDrawCallback m_drawBeforeGaugesCallback;
  void drawAfterGaugesCallback();
  XPInnDrawCallback m_drawAfterGaugesCallback;

  class Plane
  {
  public:
    Plane();

    XPInnObject m_model;
    VconMsgPlanePosition m_mostRecentMsg;
    float m_mostRecentMsgTime;

    XPLMDrawInfo_t m_drawInfo;
    float m_distSqr;
    bool m_inTCASRange;

    class SquaredDistanceComparator
    {
    public:
      typedef bool result_type;
      typedef Plane const* first_argument_type;
      typedef Plane const* second_argument_type;
      bool operator() (Plane const* a, Plane const* b) { return a->m_distSqr < b->m_distSqr; }
    };
  };
  typedef std::map<std::string, Plane> map_type;
  map_type m_map;
  typedef std::set<Plane const*, Plane::SquaredDistanceComparator> set_type;
  set_type m_TCASplanes;

  void update(Plane& plane);
  void snapToGround(Plane& plane);
  void deduceAnimState(Plane& plane);
  void draw(Plane& plane);

  XPInnPlanesDataRefs m_datarefs;
  XPInnArrayDataRef<xplane::data::sim::operation::override::override_planepath> m_overrideDataRef1;
  XPInnArrayDataRef<xplane::data::sim::operation::override::override_plane_ai_autopilot> m_overrideDataRef2;
  XPInnDataRef<xplane::data::sim::flightmodel::position::latitude> m_latitudeDataRef;
  XPInnDataRef<xplane::data::sim::flightmodel::position::longitude> m_longitudeDataRef;
  XPInnDataRef<xplane::data::sim::flightmodel::position::elevation> m_elevationDataRef;
};

#endif //__XPINN__PLANES_H__